package com.skywolf.chem.common.config;

import com.skywolf.chem.common.interceptor.AuthenticationInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

//import com.skywolf.chem.common.interceptor.AuthorizationInterceptor;

/**
 * web配置类
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {

    //    @Resource
//    private AuthorizationInterceptor authorizationInterceptor;
    @Resource
    private AuthenticationInterceptor authenticationInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(authenticationInterceptor)
                .excludePathPatterns(
                        "/ct/user/login-info/**",
                        "/ct/article/article/**",
                        "/ct/article/contest-notice/**",
                        "/ct/article/introduce-info/**",
                        "/ct/article/live-info/**",
                        "/ct/comments/comment-info/getCommentByArticleId",
                        "/ct/comments/comment-info/commentPraise",
                        "/ct/comments/comment-rev-info/**",
                        "/ct/pageconfig/**",
                        "/ct/user/login-info/**",
                        "/ct/usercenter/suggest-info/**",
                        "/manager/file/uploadImg",
                        "/ct/ct-config/**",

                        "/usercenter/login/**",
                        "/pageconfig/loop-img/**",
                        "/usercenter/user/reset-password/**",
                        "/v2/api-docs",//swagger api json
                        "/swagger-resources/configuration/ui",//用来获取支持的动作
                        "/swagger-resources",//用来获取api-docs的URI
                        "/swagger-resources/configuration/security",//安全选项
                        "/swagger-ui.html",
                        "/webjars/**",
                        "/error",
                        "/",
                        "/csrf",
                        "/news/news/**",
                        "/product/product/**",
                        "/recruitment/recruitment/**",
                        "/home/banner/**",
                        "/static/**")
                .addPathPatterns("/**");
//        registry.addInterceptor(authorizationInterceptor)
//                .excludePathPatterns("/login/**",
//                        "/v2/api-docs",//swagger api json
//                        "/swagger-resources/configuration/ui",//用来获取支持的动作
//                        "/swagger-resources",//用来获取api-docs的URI
//                        "/swagger-resources/configuration/security",//安全选项
//                        "/swagger-ui.html",
//                        "/webjars/**",
//                        "/error",
//                        "/",
//                        "/csrf",
//                        "/static/**")
//                .addPathPatterns("/**");
    }

//    /**
//     * Configure content negotiation options.
//     *
//     * @param configurer
//     */
//    @Override
//    public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
////
////        configurer.defaultContentType(MediaType.APPLICATION_JSON)
////        .mediaType("json", MediaType.APPLICATION_JSON);
//
//    }

//    @Override
//    public void addCorsMappings(CorsRegistry registry) {
//        registry.addMapping("/**")
//                //是否发送Cookie
//                .allowCredentials(true)
//                //放行哪些原始域
//                .allowedOrigins("*")
//                .allowedMethods(new String[]{"GET", "POST", "PUT", "DELETE"})
//                .allowedHeaders("*")
//                .exposedHeaders("Content-Type", "X-Requested-With", "accept", "Origin", "Access-Control-Request-Method", "Access-Control-Request-Headers");
//    }

    private CorsConfiguration corsConfig() {
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        /* 请求常用的三种配置，*代表允许所有，当时你也可以自定义属性（比如header只能带什么，只能是post方式等等）
        */
        corsConfiguration.addAllowedOrigin("*");
        corsConfiguration.addAllowedHeader("*");
        corsConfiguration.addAllowedMethod("*");
        corsConfiguration.setAllowCredentials(true);
        corsConfiguration.setMaxAge(3600L);
        return corsConfiguration;
    }

    @Bean
    public CorsFilter corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", corsConfig());
        return new CorsFilter(source);
    }
}
